<template>
  <el-dialog
    v-model="dialogOverflowVisible"
    title="新增角色"
    width="800"
    he
    draggable
    overflow
  >
    <!-- 表格 -->
    <div>
      <PureTableBar title="角色列表" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <el-button type="primary" :icon="useRenderIcon(AddFill)">
            新增角色
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            align-whole="center"
            showOverflowTooltip
            table-layout="auto"
            max-height="300px"
            :size="size"
            adaptive
            :data="tableData"
            :columns="dynamicColumns"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
              >
                修改
              </el-button>
              <el-popconfirm
                :title="`是否确认删除角色名称为${row.name}的这条数据`"
              >
                <!-- @confirm="handleDelete(row)" -->
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
              <el-popconfirm
                width="220"
                :icon="InfoFilled"
                icon-color="#626AEF"
                title="勾选配置权限"
                @cancel="() => {}"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Menu)"
                    @click="handleMenu(row)"
                  >
                    权限
                  </el-button>
                </template>
                <template #actions="{ confirm, cancel }">
                  <el-checkbox-group
                    v-model="permissionList"
                    style="display: flex; flex-direction: column"
                  >
                    <el-checkbox label="Option A" value="Value A" />
                    <el-checkbox label="Option B" value="Value B" />
                    <el-checkbox label="Option C" value="Value C" />
                  </el-checkbox-group>
                  <el-button size="small" @click="cancel">No!</el-button>
                  <el-button type="danger" size="small" @click="confirm">
                    Yes?
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogOverflowVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogOverflowVisible = false">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { PaginationProps } from "@pureadmin/table/dist/types/index.js";
import { reactive, ref } from "vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import { PureTableBar } from "@/components/RePureTableBar";
import Menu from "@iconify-icons/ep/menu";
import { InfoFilled } from "@element-plus/icons-vue";
defineOptions({
  name: "RoleMgmtModal"
});

const dialogOverflowVisible = ref(false);

const userFormdata = ref({
  useName: "",
  role: "",
  telNumber: ""
});

const treeRef = ref();
const isShow = ref(false);
const permissionList = ref([]);

async function handleMenu(row?: any) {
  const { id } = row;
  isShow.value = true;
  if (id) {
    isShow.value = true;
    // const { data } = await getRoleMenuIds({ id });
    // treeRef.value.setCheckedKeys(data);
  } else {
    isShow.value = false;
  }
}

const columns: TableColumnList = [
  {
    label: "角色",
    prop: "role"
  },
  {
    label: "权限",
    prop: "name"
  },
  {
    label: "地址",
    prop: "address"
  },
  {
    label: "操作",
    fixed: "right",
    width: 210,
    slot: "operation"
  }
];
const tableData = [
  {
    name: "查看权限",
    address: "No. 189, Grove St, Los Angeles",
    date: "2024-1-3",
    role: "财务"
  },
  {
    name: "查看权限，修改权限",
    address: "No. 189, Grove St, Los Angeles",
    date: "2024-1-3",
    role: "总经理"
  }
];

const pagination = reactive<PaginationProps>({
  total: 0,
  pageSize: 10,
  currentPage: 1,
  background: true
});

const treeData = ref([{ label: "123" }]);

function handleSizeChange(val: number) {
  console.log(`${val} items per page`);
}

function handleCurrentChange(val: number) {
  console.log(`current page: ${val}`);
}

function handleSelectionChange(val) {
  console.log("handleSelectionChange", val);
}

function onSearch(val) {
  console.log("handleSelectionChange", val);
}

defineExpose({ dialogOverflowVisible });
</script>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
.add-role-modal {
  max-height: 500px;
}
</style>
